import { defineStore } from "pinia";
export const useScriptStore = defineStore({
  id: "scriptStore",
  state: () => ({
    scripts: []
  }),
  actions: {
    addScript(script) {
      this.scripts = script;
    },
    removeScript(id) {
      this.scripts = this.scripts.filter(scripts => scripts.id !== id);
    },
    getScriptById(id) {
      // console.log(typeof this.scripts);
      if (localStorage.getItem("scriptStore") != null) {
        // console.log(localStorage.getItem("scriptStore"));
        let scriptList = JSON.parse(
          localStorage.getItem("scriptStore")
        ).scripts;
        // console.log(scriptList);
        return scriptList.find(item => item.id === Number(id));
      }
      return [];
    }
  },
  persist: {
    strategies: [
      {
        key: "scriptStore",
        storage: localStorage
      }
    ],
    enabled: true
  } // 开启本地缓存
});
